package com.serviceprovider.business.module.admin.action.services.dao.impl;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.action.model.AclAction;
import com.serviceprovider.business.module.admin.action.services.dao.interfaces.IAclActionService;
import com.serviceprovider.business.module.common.Page;
import com.serviceprovider.business.module.common.PaginationHelper;
import com.serviceprovider.business.utility.AppUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Service;

//~--- JDK imports ------------------------------------------------------------

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Owat
 *
 */
@Service("aclActionService")
public class AclActionServiceImpl extends JdbcDaoSupport implements IAclActionService {

    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass());

    @Autowired
    public AclActionServiceImpl(@Qualifier("jdbcTemplate") JdbcTemplate jdbcTemplate) {
        setJdbcTemplate(jdbcTemplate);
    }

    public Page<AclAction> findAll(int pageNo, int recordSize, String sortColumn, String sortOrder) {
        PaginationHelper<AclAction>       pageHelper = new PaginationHelper<AclAction>();
        ParameterizedRowMapper<AclAction> mapper     = new AclActionRowMapper();
        String                            sql        = "select id,name,description,active  from acl_action order by " + sortColumn + " " + sortOrder;    // $NON-NLS-1$
        String countSql = "SELECT COUNT(*) FROM acl_action";

        return pageHelper.fetchPage(getJdbcTemplate(), countSql, sql, new Object[] {}, pageNo, recordSize, sortColumn,
                                    sortOrder, mapper);
    }

    @SuppressWarnings("rawtypes")
    public List<AclAction> findAll() {
        String          sql  = "select id,name,description,active  from acl_action";    // $NON-NLS-1$
        List            list = getJdbcTemplate().queryForList(sql);
        List<AclAction> ret  = new ArrayList<AclAction>();
        Map             row  = null;
        AclAction       aux  = null;

        for (Object obj : list) {
            row = (Map) obj;
            aux = new AclAction();
            aux.setId(Integer.valueOf(String.valueOf(row.get("id"))));    // $NON-NLS-1$
            aux.setName(String.valueOf(row.get("name")));                 // $NON-NLS-1$
            aux.setDescription(String.valueOf(row.get("description")));
            aux.setActive(AppUtil.getBoolean(row.get("active")));
            ret.add(aux);
        }

        return ret;
    }

    /*
     * (non-Javadoc)
     *
     * @see springjdbc.example.service.RoleService#getRole(int)
     */
    public AclAction getAclActionById(int id) {
        RowMapper mapper = new RowMapper() {
            public AclAction mapRow(ResultSet rs, int rowNum) throws SQLException {
                AclAction role = new AclAction();

                AclActionServiceImpl.mapRow(role, rs);

                return role;
            }
        };
        final String sql = "select id,name,description,active  from acl_action where id = ?";    // $NON-NLS-1$

        try {
            return (AclAction) getJdbcTemplate().queryForObject(sql, new Object[] { id }, mapper);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    private static void mapRow(AclAction obj, ResultSet rs) throws SQLException {
        obj.setId(rs.getInt("id"));                         // $NON-NLS-1$
        obj.setName(rs.getString("name"));                  // $NON-NLS-1$
        obj.setDescription(rs.getString("description"));    // $NON-NLS-1$
        obj.setActive(AppUtil.getBoolean(rs.getString("active")));
    }

    /*
     * (non-Javadoc)
     *
     * @see springjdbc.example.service.RoleService#insertRole(springjdbc.example. domain.Role)
     */
    public void insertAclAction(AclAction obj) {
        final String sql = "insert into acl_action (name,description,active) values(?,?,?)";    // $NON-NLS-1$

        getJdbcTemplate().update(sql, new Object[] { obj.getName(), obj.getDescription(), obj.isActive() });
    }

    public void updateAclAction(AclAction obj) {
        final String sql = "update acl_action set name = ? , description = ? , active = ? where id = ?";    // $NON-NLS-1$

        getJdbcTemplate().update(sql,
                                 new Object[] { obj.getName(), obj.getDescription(), obj.isActive(), obj.getId() });
    }

    public void deleteAclActionsById(int[] idList) {
        final String sql = "delete from acl_action where id = ?";    // $NON-NLS-1$

        for (int i = 0; i < idList.length; i++) {
            getJdbcTemplate().update(sql, new Object[] { new Integer(idList[i]) });
        }
    }

    /*
     * (non-Javadoc)
     *
     * @see springjdbc.example.service.RoleService#findAll()
     */
    private static class AclActionRowMapper implements ParameterizedRowMapper<AclAction> {
        @Override
        public AclAction mapRow(ResultSet rs, int index) throws SQLException {
            AclAction aclAction = new AclAction();

            aclAction.setId(rs.getInt("id"));                         // $NON-NLS-1$
            aclAction.setName(rs.getString("name"));                  // $NON-NLS-1$
            aclAction.setDescription(rs.getString("description"));    // $NON-NLS-1$
            aclAction.setActive(AppUtil.getBoolean(rs.getString("active")));

            return aclAction;
        }
    }
}



